--专门为TcpChannel用的带超时的请求，因为其他游戏状态超时通过ping来实现，这里ping不通，而且所有调用都不是rpc且没有状态
SendAndWaitRequest = class('SendAndWaitRequest')
SendAndWaitRequest.TIMEOUT = 20

function SendAndWaitRequest:ctor(msgID, content, timeout)
    self.SendContent = content
    self.MsgID = msgID
    self.timeout = timeout or SendAndWaitRequest.TIMEOUT
    self.response = nil
    self.errorCode = 0
    self.finish = false
end

function SendAndWaitRequest:_Send()
    Channels.TcpChannel:Send(self.MsgID, self.SendContent)
end

function SendAndWaitRequest:SendAsync()
    local time = 0
    self:_Send()

    local OnReceiveRequest = function(msg)
        self.response = msg.data[1]
        self.finish = true
    end

    local OnReceiveError = function(msg)
        self.errorCode = msg.data
        self.finish = true
    end

    EVTM.add(EVENT.SYS_TCP_CONNECT_FINISH, self.onTcpConnect, self)
    EVTM.add(EVENT.SYS_TCP_DISCONNECTED, self.onTcpDisconnect, self)
    EVTM.add(EVENT.SYS_TCP_RECEIVE_MSG..tostring(self.MsgID), OnReceiveRequest)
    EVTM.add(EVENT.SYS_TCP_RECEIVE_ERR..tostring(self.MsgID), OnReceiveError)

    while true do
        if time >= self.timeout and not self.finish then --超时等待重试
            Channels.TcpChannel.tcpChannel:Close()
            time = 0
            Channels.TcpChannel:OnDisconnect()
        else
            if self.finish then
                break
            end

            coroutine.step()
            time = time + UnityEngine.Time.fixedDeltaTime
        end
    end

    EVTM.remove(EVENT.SYS_TCP_CONNECT_FINISH, self.onTcpConnect, self)
    EVTM.remove(EVENT.SYS_TCP_DISCONNECTED, self.onTcpDisconnect, self)
    EVTM.remove(EVENT.SYS_TCP_RECEIVE_MSG..tostring(self.WaitMsgID), OnReceiveRequest)
    EVTM.remove(EVENT.SYS_TCP_RECEIVE_ERR..tostring(self.MsgID), OnReceiveError)
end

function SendAndWaitRequest:onTcpConnect()
    self:_Send()
end

function SendAndWaitRequest:onTcpDisconnect()
    Channels.TcpChannel:Connect(config.GAME_TCP_HOST, config.GAME_TCP_PORT)
end